

package Persistance;


import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult; 

/**
 * This class contains static methods such as 
 * read, right, get and set attribute values etc.
 * which manage the access to the XML files
 * @author	Arik Goldstein & Yaniv Elia & Dudi Patimer
 * @version	1.0
 * @since	1.0
 */
public class XMLUtilityManagment {
   
 /**
 * Reads from XML file
 *
 * @param      filename    The name of the file to read
 * @return     document represents the file in DOM format
 * @since	1.0
 */    
    public static Document readXMLFile( String filename){
             
        DocumentBuilder builder;
             DocumentBuilderFactory factory =
             DocumentBuilderFactory.newInstance();
             try {
                    builder= factory.newDocumentBuilder();
                    Document document = builder.parse(filename);
                    return document;
             }catch(Exception e){
                    System.out.println("Cannot read from the XML file ");
             }
             return null;
    }
    
 /**
 * Writes into XML file
 *
 * @param      node    The root node of the saved document
 * @param      filename  The name of the saved file
 * @since	1.0
 */    
    public static void writeXMLFile(Node node,String filename){
            
        try{
                // Prepare the DOM document for writing
                 DOMSource source = new DOMSource(node);
                 
                 // Prepare the output file
                
                StreamResult result = new StreamResult(filename);
                
                // Write the DOM document to the file
                Transformer xformer = TransformerFactory.newInstance().newTransformer();
                xformer.transform(source, result);

          } catch (TransformerConfigurationException e) {
          } catch (TransformerException e) {}
    }
    
 /**
 * Returns the text content of the element whose name is toSearch
 * @return  the text content of the node (null when fails)
 * @param      node    The parent node to search in
 * @param      toSearch     The name of the requested element node
 * @since	1.0
 */    
    public static String getAttributeValue(Node node, String toSearch){
        Node attributeNode = getAttribute(node,toSearch);
        if (attributeNode==null) return null;
        else return attributeNode.getTextContent();
    }
    
 /**
 * Returns integer value represents the text content of the element whose name is toSearch
 * @return  the int value of the node (-1 when fails)
 * @param      node    The parent node to search in
 * @param      toSearch     The name of the requested element node
 * @since	1.0
 */    
    public static int getAttributeIntValue(Node node, String toSearch){
        Node attributeNode = getAttribute(node,toSearch);
        if (attributeNode==null) return -1;
        else return new Integer(attributeNode.getTextContent()).intValue();
    }
    
 /**
 * Returns double value represents the text content of the element whose name is toSearch
 * @return  the double value of the node (-1 when fails)
 * @param      node    The parent node to search in
 * @param      toSearch     The name of the requested element node
 * @since	1.0
 */    
    public static double getAttributeDoubleValue(Node node, String toSearch){
        Node attributeNode = getAttribute(node,toSearch);
        if (attributeNode==null) return -1;
        else return new Double(attributeNode.getTextContent()).doubleValue();
    }
    
 /**
 * Sets text content of an element node
 * @return  0 for success, -1 for failure
 * @param      node              The parent node to search in
 * @param      attributeName     The name of the requested element node
  *@param      value             The new value
 * @since	1.0
 */    
     public static int setAttributeValue(Node node, String attributeName, String value){
        Node attributeNode = getAttribute(node,attributeName);
        if (attributeNode==null) return -1;
        else{
            attributeNode.setTextContent(value);
            return 0;
        }
    }
    
 /**
 * Returns  the element whose name is AttributeName
 * @return  the  node (null when fails)
 * @param      node    The parent node to search in
 * @param      AtttributeName     The name of the requested element node
 * @since	1.0
 */    
    public static Node getAttribute(Node parent, String AttributeName){
         NodeList nl = parent.getChildNodes();
        for (int i=0;i<nl.getLength();i++){
            Node handledNode = nl.item(i);
            if (handledNode.getNodeName().compareTo(AttributeName)==0){
                return handledNode;
            }
        }
        return null;
    }
}
